package com.xhwl.centiotdata.pushtrigger.alarm.subscribe;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.DaHuaIccConstant;
import com.xhwl.centiotdata.pollingtrigger.ScheduleDispatcher;
import com.xhwl.centiotdata.service.integrateconfig.AlarmIntegrationConfig;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.dto.cent.alarm.AlarmIntegrationConfigDTO;
import com.xhwl.common.dto.cent.alarm.dahuaicc.ICCResult;
import com.xhwl.common.dto.cent.alarm.dahuaicc.ICCSubscribeListResult;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.utils.RSAUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

@Slf4j
@Component
public class HaikangISCSubscribe implements ScheduleDispatcher {

    @Autowired
    AlarmIntegrationConfig alarmIntegrationConfig;

    private static final List<AlarmIntegrationConfigDTO> integrationConfigList = new ArrayList<>();

    @Override
    public void doOneMinutesTask() {

    }

    @Override
    public void doTenMinutesTask () {
        ThreadPoolUtils.alarmExecutor.execute(this::getToken);
    }

    @EventListener
    public void listenIntegrationConfig(AlarmIntegrationConfigEvent event) {
        ThreadPoolUtils.alarmExecutor.execute(this::getToken);
    }

    private void getToken() {
        syncConfig();
        for (AlarmIntegrationConfigDTO configDTO : integrationConfigList) {
            try {
                log.info("HaikangISC 开始订阅 ===================: {}", configDTO);
                if (StringUtils.isEmpty(configDTO.getToken())) {
                    String token = login(configDTO);
                    if (!StringUtils.isEmpty(token)) {
                        configDTO.setToken(token);
                        alarmSubscribe(configDTO);
                    }
                } else {
                    configDTO.setToken(login(configDTO));
                }
            }
            catch (Exception e) {
                log.error("HaikangISC 订阅失败 {}", e.getMessage());
            }
        }
    }

    private void syncConfig() {
        List<AlarmIntegrationConfigDTO> list = alarmIntegrationConfig.getIntegrationProductConfig(
                null, ProductEnum.HAIKANG_ISC.getId());
        for (AlarmIntegrationConfigDTO i : list) {
            if (ObjectUtils.isEmpty(i)) {
                log.error("HaikangISC 告警集成配置为空，{}", i);
                continue;
            }
            for (AlarmIntegrationConfigDTO j : integrationConfigList) {
                if (Objects.nonNull(j.getToken()) && Objects.equals(j.getRowId(), i.getRowId())) {
                    i.setToken(j.getToken());
                }
            }
        }
        integrationConfigList.clear();
        integrationConfigList.addAll(list);
    }

    public static void alarmSubscribe(AlarmIntegrationConfigDTO configDTO) {
        if (StringUtils.isEmpty(configDTO.getAlarmCallback())) {
            log.info("HaikangISC 告警订阅地址为空，{}", configDTO);
            configDTO.setToken(null);
            return;
        }

        try {

        } catch (Exception e) {
            configDTO.setToken(null);
            log.error("HaikangISC 告警订阅 失败: {}，{}", configDTO, e);
        }
    }

    private static String login(AlarmIntegrationConfigDTO configDTO) {
        return null;
    }

}
